﻿@using Telerik.Web.Mvc.UI;
@using Telerik.Web.Mvc;

@(Html.Telerik().TreeView()
        .Name("AjaxTreeView")
        .DataBinding(dataBinding => dataBinding
            .Ajax().Enabled(true).Select("_TreeViewLoading", "Main")
        )
        .ClientEvents(events => events.OnLoad("AjaxTreeView_OnLoad").OnNodeDrop("OnNodeDrop"))
        .DragAndDrop(true)
)
@{ Html.Telerik().ScriptRegistrar()
    .OnDocumentReady(@<text>
    function OnNodeDrop(e) {
        var treeview = $('#AjaxTreeView').data("tTreeView");

        var item = treeview.getItemValue(e.item);
        var destination = treeview.getItemValue(e.destinationItem);
        var data = {};
        data.item = item;
        data.destinationitem = destination;
        data.position = e.dropPosition;

        $.ajax({
            cache:false,
            async:false,
            url: "@Url.Action("_TreeDrop","Main")",
            data: data,
            type: "POST",
            success: function (msg) {
                if(msg == "true") 
                {
                    return true;
                }
                else 
                {
                    alert("Not valid parent");
                    e.preventDefault();
                }
            }
        });

    }

    $(".t-link").live("click", function () {
        if( !endsWith($(this).attr("href"),"#")) $("#indexViewIframe").attr("src", $(this).attr("href"));
        return false;
    });

</text>);}